package utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;

public class HTML extends Activity {

    public static String getHTML(String URL) throws Exception {

	URL web = new URL(URL);
	URLConnection conn = web.openConnection();
	BufferedReader in = new BufferedReader(new InputStreamReader(
		conn.getInputStream()));
	StringBuffer inputResponse = new StringBuffer();
	String inputLine;
	while ((inputLine = in.readLine()) != null) {

	    inputResponse.append(inputLine);
	}
	in.close();

	return inputResponse.toString();
    }

    public static String doConnection(String URL) throws Exception {

	String html;

	URL web = new URL(URL);
	URLConnection conn = web.openConnection();
	String encoding = conn.getContentEncoding();
	if (encoding == null)
	    encoding = "ISO-8859-1";

	BufferedReader in = new BufferedReader(new InputStreamReader(
		conn.getInputStream(), encoding));
	StringBuffer inputResponse = new StringBuffer();
	String inputLine;
	while ((inputLine = in.readLine()) != null) {

	    inputResponse.append(inputLine);
	}
	in.close();

	html = inputResponse.toString();

	return html;
    }

    public static String extractXml(String html, String initTag, String endTag) {

	int beginIndex = html.indexOf(initTag);
	int endIndex = html.indexOf(endTag);
	html = html.substring(beginIndex, endIndex + endTag.length());

	return html;
    }
}
